package com.atguigu.juc.day03.pool;

import java.util.UUID;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

public class Demo01BlockingQueue {

    public static void main(String[] args) {

        // 1、创建阻塞队列对象
        BlockingQueue breadShop = new ArrayBlockingQueue(3);

        // 2、创建线程负责烤面包
        new Thread(()->{
            while (true) {
                try {
                    String bread = UUID.randomUUID().toString().replace("-", "").substring(0, 5).toUpperCase();
                    breadShop.put(bread);
                    System.out.println("【烤面包】bread = " + bread);
                    TimeUnit.SECONDS.sleep(3);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }, "thread-a").start();

        // 3、创建线程负责买面包
        new Thread(()->{

            while (true) {
                try {
                    Object bread = breadShop.take();
                    System.out.println("【买面包】bread = " + bread);
                    TimeUnit.SECONDS.sleep((long) (Math.random()*10));
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }

        }, "thread-a").start();

    }

}
